﻿using Corina.Enumerations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Corina.Infrastructure
{
    public class TransInfo : NotificationObject
    {
        #region Construct
        public TransInfo() { }

        public TransInfo(MessageTypeValue messageType, string source, List<object> paras, List<string> target, bool report = true)
        {
            MessageType = messageType;
            Source = source;
            Paras = paras;
            Target = target;
            Report = report;
        }

        public TransInfo(MessageTypeValue messageType, string source, params object[] paras)
        {
            MessageType = messageType;
            Source = source;
            Paras.AddRange(paras);
        }
        #endregion

        #region Fields
        private MessageTypeValue _messageType = MessageTypeValue.DeviceMeasure;
        private string _messageId = System.Guid.NewGuid().ToString();
        private List<object> _paras = new List<object>();
        private List<string> _target = new List<string>();
        private string _source = string.Empty;
        private bool _report = true;
        #endregion

        #region Properties
        public string MessageId
        {
            get { return _messageId; }
            set
            {
                _messageId = value;
                RaisePropertyChanged(() => MessageId);
            }
        }

        public MessageTypeValue MessageType
        {
            get { return _messageType; }
            set
            {
                _messageType = value;
                RaisePropertyChanged(() => MessageType);
            }
        }

        public List<object> Paras
        {
            get { return _paras; }
            set
            {
                _paras = value;
                RaisePropertyChanged(() => Paras);
            }
        }

        public List<string> Target
        {
            get { return _target; }
            set
            {
                _target = value;
            }
        }

        public string Source
        {
            get { return _source; }
            set
            {
                _source = value;
                RaisePropertyChanged(() => Source);
            }
        }

        public bool Report
        {
            get { return _report; }
            set
            {
                _report = value;
                RaisePropertyChanged(() => Report);
            }
        }
        #endregion

        public string XmlString()
        {
            StringBuilder builder = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            builder.Append("<msg>\r\n");
            builder.AppendFormat("<MessageId>{0}</MessageId>\r\n", MessageId);
            builder.AppendFormat("<MessageType>{0}</MessageType>\r\n", (int)MessageType);
            builder.AppendFormat("<Source>{0}</Source>\r\n", Source);
            builder.AppendFormat("<Report>{0}</Report>\r\n", Report);

            builder.Append("<to>\r\n");
            foreach (string to in Target)
            {
                builder.AppendFormat("<Target>{0}</Target>\r\n", to);
            }
            builder.Append("</to>\r\n");
            builder.Append("<CommandParams>\r\n");
            foreach (string param in Paras)
            {
                builder.AppendFormat("<Param>{0}</Param>\r\n", param);
            }
            builder.Append("</CommandParams>\r\n</msg>");
            return builder.ToString();
        }

        public byte[] GetBuffer()
        {
            return Encoding.UTF8.GetBytes(XmlString());
        }
    }
}
